package com.fernfx.xingtan.common.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.view.dialog.ConProgressDialog;

import java.util.Map;

import butterknife.ButterKnife;


public abstract class BaseFragment extends Fragment {

    protected String TAG = getClass().getSimpleName();

    protected Activity activity;
    protected View rootView;
    private ConProgressDialog mProgressDialog;

    public Map<String, Object> requestArgsMap = Constant.RequestArgs.REQUEST_ARGS_MAP;

    public BaseFragment() {
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutResId(), container, false);
        initButterKnife(rootView);
        init(savedInstanceState);   //兼容IM列表.
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
        initListener();
    }

    /*
    *init butterKnife
    */
    protected void initButterKnife(View view) {
        ButterKnife.bind(this,view);
    }

    protected void init(Bundle savedInstanceState) {
    }

    public void onFragmentShown() {
    }

    protected abstract int getLayoutResId();

    protected void initView() {
    }

    protected void initData() {
    }

    protected void initListener() {
    }

    public void showLoading() {
        if (mProgressDialog == null) {
            if (null != getActivity()) {
                mProgressDialog = new ConProgressDialog(getActivity());
                mProgressDialog.setMessage(getString(R.string.loading));
            }
        }
        if (null != mProgressDialog) {
            mProgressDialog.show();
        }
    }

    public void hideLoading() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.cancel();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
